<template>
	<view class="container">
		<view class="tabs">
			<view class="tab-item" :class="{ active: tab == index }" v-for="(item, index) in tabs" :key="index" @click="onTab(index)">
				{{ item.text }}
			</view>
		</view>
		<view class="item" v-if="list.length > 0" v-for="(item, index) in list" :key="index">
			<view class="flex info-box">
				<view class="img">
					<image
						v-if="item.images && item.images.length > 0"
						:src="oss(item.images[0].url, 180, 180)"
						mode="aspectFill"
						class="image"
					></image>
					<view class="image-placeholder" v-else><text class="placeholder-text">暂无图片</text></view>
					<view class="level-tag">
						<image v-if="item.expire_days == 7" src="/static/images/sevenday.png" style="width: 76rpx; height: 76rpx"></image>
						<image v-if="item.expire_days == 30" src="/static/images/ssday.png" style="width: 76rpx; height: 76rpx"></image>
						<image v-if="item.expire_days == 90" src="/static/images/jsday.png" style="width: 76rpx; height: 76rpx"></image>
						<image v-if="find.expire_days == 365" src="/static/images/oneyearday.png" style="width: 76rpx; height: 76rpx"></image>
					</view>
				</view>
				<view class="left">
					<view class="title flex">
						<view>{{ item.title }}</view>
						<template v-if="item.pay_status == 0">
							<view class="status unpay">待支付</view>
						</template>
						<view v-else class="status" :class="[`status${item.status}`]">
							{{ item.status === 0 ? '待审核' : item.status === 1 ? '审核通过' : '已过期' }}
						</view>
					</view>
					<view class="desc">{{ item.content }}</view>
					<view class="time-box">
						<view class="time-item">
							<text>发布时间：</text>
							<text>{{ formateDate(item) }}</text>
						</view>
						<view class="time-item">
							<image src="/static/images/addr.png" mode="widthFix" style="width: 30rpx"></image>
							<text>{{ item.area }}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="footer">
				<view class="day">
					剩余
					<text :class="{ 'text-warning': item.remain_time <= 3, 'text-danger': item.remain_time <= 0 }">{{ item.remain_time }}</text>
					天
				</view>
				<view class="btns">
					<button class="btn edit" v-if="item.remain_time > 0 && item.pay_status == 1" @click="editlocaldata(item, index)">修改</button>
					<button
						class="btn down"
						v-if="item.pay_status == 1 && item.remain_time > 0 && item.upordown == 1"
						@click="changeupordown(item, index)"
					>
						下架
					</button>
					<button
						class="btn up"
						v-if="item.pay_status == 1 && item.remain_time > 0 && item.upordown == 0"
						@click="changeupordown(item, index)"
					>
						上架
					</button>
					<button class="btn" v-if="item.pay_status == 0" @click="onPay(item)">立即支付</button>
					<button class="btn del" v-if="item.remain_time <= 0 || item.pay_status == 0" @click="dellocaldata(item, index)">删除</button>
				</view>
			</view>
		</view>
		<wanl-empty v-if="list.length === 0" :text="empty" />
	</view>
</template>
<script>
import { formatTimestamp } from '@/utils/index.js';

export default {
	data() {
		return {
			tabs: [
				{
					text: '全部',
					query: {}
				},
				{
					text: '已发布',
					query: {
						pay_status: 1,
						upordown: 1,
						status: 1
					}
				},
				{
					text: '待支付',
					query: {
						pay_status: 0
					}
				},
				{
					text: '审核中',
					query: {
						pay_status: 1,
						status: 0
					}
				}
			],
			query: {},
			tab: 0,
			list: [],
			empty: '暂无发表动态'
		};
	},
	onLoad() {
		this.loadData();
	},
	onPullDownRefresh() {
		this.loadData();
	},
	methods: {
		formateDate(item) {
			return formatTimestamp(item.createtime);
		},
		onTab(index) {
			this.tab = index;
			this.query = this.tabs[index].query;
			this.loadData();
		},
		onPay(item) {
			uni.redirectTo({
				url: '/pages/local/pay?price_id=' + item.price_id + '&local_id=' + item.id
			});
		},
		editlocaldata(item) {
			uni.navigateTo({
				url: '/pages/local/postDetail?detail=' + JSON.stringify(item)
			});
			uni.setStorageSync('myDetail', JSON.stringify(item));
		},
		dellocaldata(item, index) {
			let that = this;
			uni.showModal({
				title: '提示',
				content: '您确定要此条本地生活的信息吗?',
				success(res) {
					if (res.confirm) {
						//do
						uni.request({
							url: '/wanlshop/local_life/delLocalLife',
							method: 'post',
							data: { id: item.id },
							success(res) {
								that.list.splice(index, 1);
							}
						});
					} else {
						//cancel
					}
				}
			});
		},

		changeupordown(item, index) {
			console.log('########', item, index, 'index');
			let that = this;
			uni.request({
				url: '/wanlshop/local_life/changeupordown',
				method: 'post',
				data: {
					id: item.id
				},
				success(res) {
					console.log('!!!!!!!!', res);
					console.log(res.res);
					if (res.res.code == 1) {
						//修改成功
						if (item.upordown == 0) {
							item.upordown = 1;
						} else {
							item.upordown = 0;
						}
						let newitem = item;
						console.log('@@@@', newitem);
						that.list.splice(index, 1, newitem);
					}
				}
			});
		},

		async loadData() {
			uni.stopPullDownRefresh();
			await uni.request({
				url: '/wanlshop/local_life/myList',
				type: 'get',
				data: this.query,
				success: res => {
					this.list = res.data.data;
					console.log(res);
				}
			});
		},

		oss(url, w = 120, h = 120, modenum = 2, type = '', format = 'jpg', resize = 'resize', multiple = 3) {
			let ossStc = this.$store.state.common.appUrl.oss;
			let image = '';
			let mode = ['m_lfit', 'm_mfit', 'm_fill', 'm_fixed'];
			// 图像，自适应方向：1，渐进显示：1，转换格式：jpg，图片质量：q_90，图片锐化：50，浓缩图
			let rule = [
				'?x-oss-process=image',
				'auto-orient,1',
				'interlace,1',
				'format,jpg',
				'quality,q_90',
				'sharpen,50',
				'resize,m_fixed,w_120,h_120'
			];
			// 转换格式
			if (format == 'png') {
				rule[3] = ['format', 'png'].join(',');
			}
			// 判断是否有高度
			if (w == 0) {
				rule[6] = [resize, mode[modenum], `h_${h * multiple}`].join(',');
			} else if (h == 0) {
				rule[6] = [resize, mode[modenum], `w_${w * multiple}`].join(',');
			} else {
				rule[6] = [resize, mode[modenum], `w_${w * multiple}`, `h_${h * multiple}`].join(',');
			}
			//当地址不存在
			if (url) {
				if (/^data:image\//.test(url)) {
					image = url;
				} else if (/^(http|https):\/\/.+/.test(url)) {
					rule.pop();
					image = url + rule.join('/');
				} else {
					image = ossStc + url + rule.join('/');
				}
			} else {
				if (type == 'transparent') {
				} else if (type == 'avatar') {
					image = ossStc + '/common/mine_def_touxiang_3x.png';
				} else {
					image = ossStc + '/common/img_default3x.png';
				}
			}
			return image;
		}
	}
};
</script>
<style lang="scss" scoped>
.container {
	padding: 20rpx;
	background: #f5f5f5;
	min-height: 100vh;
	padding-top: 120rpx;
	.tabs {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #fff;
		position: fixed;
		top: 0;
		left: 0;
		padding: 20rpx 40rpx;
		width: 100%;
		z-index: 9;
		.tab-item {
			height: 60rpx;
			line-height: 60rpx;
			position: relative;
			&.active {
				border-bottom: 4rpx solid #1747cd;
			}
		}
	}
	.item {
		background: url(/static/images/info-bg.png) no-repeat;
		background-size: cover;
		padding: 14rpx 28rpx 0;
		box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.08);
		border-radius: 16rpx;
		margin-bottom: 30rpx;
	}
	.info-box {
		gap: 24rpx;
		padding-top: 15rpx;
	}
	.img {
		position: relative;
	}
	.image {
		width: 180rpx;
		height: 180rpx;
		border-radius: 12rpx;
	}
	.image-placeholder {
		width: 140rpx;
		height: 140rpx;
		border-radius: 12rpx;
		background: #ffffff;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 2rpx dashed #bdc3c7;
		order: 2;
		flex-shrink: 0;
	}

	.placeholder-text {
		font-size: 20rpx;
		color: #95a5a6;
	}
	.title {
		align-items: center;
		justify-content: space-between;
		.status {
			font-size: 22rpx;
			&.unpay {
				color: #ff6666;
			}
			&.status0 {
				color: #f39c12;
			}
			&.status1 {
				color: #27ae60;
			}
			&.status2 {
				color: #ff6666;
			}
			&.status4 {
				color: #7f8c8d;
			}
		}
	}
	.left {
		flex: 1;
	}
	.desc {
		margin-top: 20rpx;
		-webkit-line-clamp: 2;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		font-size: 24rpx;
		color: #666;
	}
	.level-tag {
		position: absolute;
		top: -8rpx !important;
		left: -8rpx !important;
	}
	.time-box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 22rpx;
		background: rgba(255, 255, 255, 0.4);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		padding: 8rpx 28rpx;
		height: 60rpx;
		margin-top: 20rpx;
		font-size: 24prx;
		color: #999;
		.time-item {
			display: flex;
			align-items: center;
		}
	}
	.footer {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 30rpx;
		border-top: 1px solid #e8e8e8;
		padding: 20rpx 0;
	}
	.day {
		font-size: 24rpx;
		color: #333;
		&.text-warning {
			color: #fdcb6e;
		}
		&.text-danger {
			color: #e17055;
		}
	}
	.btns {
		display: flex;
		gap: 20rpx;
		flex: 1;
		justify-content: flex-end;
	}
	.btn {
		font-size: 24rpx;
		border: none;
		border-radius: 12rpx;
		margin: 0;
		&:first-child {
			background: #1747cd;
			color: #fff;
		}
	}
}
</style>
